package org.tls.ylt.common.helper;


import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tls.ylt.common.config.EmailConfig;

/**
 * JavaMail发送邮件: java版本-与web无关
 * 前提是QQ邮箱里帐号设置要开启POP3/SMTP协议
 */
@Component
public class EmailHelper {

    @Autowired
    EmailConfig emailConfig;

    public String send(String toEmail) {
        String fromEmail = emailConfig.getFromEmail();
        String pwd = emailConfig.getPwd();
        String fromName = emailConfig.getFromName();
        System.out.println(fromEmail);
        System.out.println(pwd);
        Email email=new SimpleEmail();
        try {
            //邮箱的SMTP服务器，一般126邮箱的是smtp.126.com，qq邮箱为smtp.qq.com
            email.setHostName("smtp.qq.com");
            email.setCharset("utf-8");
            //收件人
            email.addTo(toEmail);
            //发件人,用户名可随便填写
            email.setFrom(fromEmail,fromName);
            //你的邮箱地址和你的stmp授权码
            email.setAuthentication(fromEmail,pwd);
            //email.addCc("18642967872@163.com");//抄送方
            email.setSubject("验证码测试邮件");
            String[] letters = new String[] {
                    "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
                    "A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
                    "0","1","2","3","4","5","6","7","8","9"};
            StringBuilder sb =new StringBuilder();
            for (int i = 0; i < 6; i++) {
                sb = sb.append(letters[(int)Math.floor(Math.random()*letters.length)]);
            }
            email.setMsg("你好,本次操作验证码为："+sb.toString()+"，有效时间3分钟");
            email.send();
            System.out.println("验证码为："+sb.toString());
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("验证码发送失败！");
            return null;
        }
    }

    public boolean send(String toEmail, String content) {
        String fromEmail = emailConfig.getFromEmail();
        String pwd = emailConfig.getPwd();
        String fromName = emailConfig.getFromName();
        System.out.println(fromEmail);
        System.out.println(pwd);
        Email email=new SimpleEmail();
        try {
            //邮箱的SMTP服务器，一般126邮箱的是smtp.126.com，qq邮箱为smtp.qq.com
            email.setHostName("smtp.qq.com");
            email.setCharset("utf-8");
            //收件人
            email.addTo(toEmail);
            //发件人,用户名可随便填写
            email.setFrom(fromEmail,fromName);
            //你的邮箱地址和你的stmp授权码
            email.setAuthentication(fromEmail,pwd);
            //email.addCc("18642967872@163.com");//抄送方
            email.setSubject("预约通知邮件");
            email.setMsg(content);
            email.send();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("通知邮件发送失败！");
            return false;
        }
    }
}